import 'package:flutter/material.dart';
import 'package:znmm/UI/A_Common/image/cached_network_Image.dart';

class MidSwiperView extends StatefulWidget {
  const MidSwiperView({super.key});

  @override
  State<MidSwiperView> createState() => _MidSwiperViewState();
}

class _MidSwiperViewState extends State<MidSwiperView> {
  final List<String> _imageList = [];
  int _selectIndex = 0;

  //自动滚动
  final PageController _controller = PageController(initialPage: 0);

  // int _iNotiId1 = -1;

  @override
  void initState() {
    super.initState();

    _imageList.add("https://www.glassesin.com/image/about/4.jpg");
    _imageList.add("https://www.glassesin.com/image/about/5.jpg");
    _imageList.add("https://www.glassesin.com/image/about/6.jpg");
  }

  @override
  void dispose() {
    _controller.dispose();
    //_timer.cancel();

    //NotifyCenter().removeCallback(_iNotiId1);

    super.dispose();
  }

  // void _initDateList() {
  //   _specialOfferModelList = HomeMag().getSwiperProducList();
  //   if (_specialOfferModelList.isEmpty) {
  //     _specialOfferModelList = HomeModData.productList;
  //   }
  // }

  @override
  Widget build(BuildContext context) {
    return _imageList.isNotEmpty
        ? Stack(children: [
            Container(
              height: 200,
              margin: const EdgeInsets.fromLTRB(5, 0, 5, 0),
              // decoration: BoxDecoration(
              //   color: Theme.of(context).splashColor,
              //   borderRadius: const BorderRadius.all(Radius.circular(10)),
              // ),
              child: PageView.builder(
                controller: _controller,
                itemBuilder: (context, index) {
                  //final prducMol = _specialOfferModelList[index];
                  return GestureDetector(
                    onTap: () {
                      // Get.to(ShoppingPage(
                      //   product: prducMol,
                      // ));
                    },
                    child: Padding(
                        padding: const EdgeInsets.fromLTRB(2, 0, 2, 0),
                        child: ClipRRect(
                          borderRadius: BorderRadius.circular(10.0),
                          child: SizedBox(
                            width: double.infinity,
                            height: double.infinity,
                            child: CachedNetworkImageLoader(
                              imageUrl: _imageList[index],
                            ),
                          ),
                        )),
                  );
                },
                itemCount: _imageList.length,
                allowImplicitScrolling: true,
                onPageChanged: (value) {
                  setState(() => _selectIndex = value);
                },
              ),
            ),
            _buildPageIndicator()
          ])
        : Container();
  }

  Widget _buildPageIndicator() {
    List<Widget> list = [];
    for (int iIndex = 0; iIndex < _imageList.length; iIndex++) {
      list.add(iIndex == _selectIndex ? _indicator(true) : _indicator(false));
    }
    return Container(
      height: 200,
      alignment: Alignment.bottomCenter,
      padding: const EdgeInsets.only(bottom: 12),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: list,
      ),
    );
  }

  Widget _indicator(bool isActive) {
    return SizedBox(
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 150),
        margin: const EdgeInsets.symmetric(horizontal: 5.0),
        height: 4.0,
        width: isActive ? 16 : 4.0,
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(2)),
          color: isActive ? Colors.blue : Colors.black,
        ),
      ),
    );
  }
}
